package com.uottawa.clickerapp.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.uottawa.clickerapp.R;
import com.uottawa.clickerapp.domain.Answer;
import com.uottawa.clickerapp.domain.Question;
import com.uottawa.clickerapp.domain.QuestionSession;
import com.uottawa.clickerapp.service.ClickerAppWebServiceClient;
import com.uottawa.clickerapp.service.impl.ClickerAppWebServiceClientImpl;

import android.app.Activity;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class QuestionList extends Activity implements OnItemClickListener {
    TextView theTitle;
    ListView listQuestions;
    String session;
    List<Question> questions;
    QuestionSession questionSession;
	  public void onCreate(Bundle icicle) {
	        super.onCreate(icicle);
	        List<String> questionStrings = new ArrayList<String>();
	        Intent fromSession = getIntent();
		    session = (String) fromSession.getSerializableExtra("SessionSelected");
	        new Thread(new Runnable() {
	            public void run() {
	            	ClickerAppWebServiceClient client =	ClickerAppWebServiceClientImpl.getInstance();
	                questionSession = client.getQuestions(session);
	            }
	          }).start();
	        try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        setContentView(R.layout.rowlayout);
		    session.concat(": Please Select a Question");
		    theTitle = (TextView) findViewById(R.id.thetitle);
		    theTitle.setText(session);
		    if (questionSession != null)
		    {
		        if (!questionSession.getQuestions().isEmpty())
		        {
			        questions = questionSession.getQuestions();
			        if (questions.size() != 0)
			        {
			        	for (int x = questions.size() - 1; x > -1; x--)
			        	{
			        		String tmp = "Question " + (questions.size()-x) + ": " + questions.get(x).getQuestionText();
			        		questionStrings.add(tmp);
			        	}
			        }
				    listQuestions = (ListView) findViewById(R.id.listnames);
			        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, questionStrings);
			        listQuestions.setAdapter(arrayAdapter); 
			        listQuestions.setOnItemClickListener(this);
		        }
		    }
	  }
	  @Override
	    public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
			String selection = (String) ((TextView) v).getText();
			Context c = v.getContext();
			try {
				Question theQuestion = questions.get(questions.size() - position - 1);
	    		//Dialog dialog = new Dialog(c);
	    		//dialog.setTitle("q: " + theQuestion.getID());
	    		//dialog.show();
				Intent goQuestion = new Intent(QuestionList.this,VoteActivity.class);
				goQuestion.putExtra("QuestionText", theQuestion.getQuestionText());
				goQuestion.putExtra("session", session);
				startActivity(goQuestion);
			}
			catch (NullPointerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }
	}


